/*******************************************************************************
 * Copyright (c) 2011 Jovansonlee B. Cesar.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Jovansonlee B. Cesar - initial API and implementation
 ******************************************************************************/
package com.ivanceras.client.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.shared.bo.Ad_tab;

public class AdWindowUi extends Composite{
	private static AdWindowUiUiBinder uiBinder = GWT
			.create(AdWindowUiUiBinder.class);

	interface AdWindowUiUiBinder extends UiBinder<Widget, AdWindowUi> {
	}

	
	public AdWindowUi(AdTabUi[] tabUiList) {
		initWidget(uiBinder.createAndBindUi(this));
		this.tabUiList = tabUiList;
		for(AdTabUi tabui : tabUiList){
			add(tabui);
		}
		if(tabUiList != null && tabUiList.length > 0){
			selectTab(0);
		}
	}

	@UiField DeckPanel tabUiPanel;	
	@UiField HTMLPanel tabUiNames;
	List<String> tabNameList = new ArrayList<String>();
	AdTabUi[] tabUiList = null;
	List<Button> buttonList = new ArrayList<Button>();

	int tabButtonWidth = 200;
	
	HashMap<String, Integer> btnNames = new HashMap<String, Integer>();

	private void add(final AdTabUi tabui){
		tabUiPanel.add(tabui);
		//tabUiList.add(tabui);
		final Ad_tab tab = tabui.getAd_tab();
		String tabName = tab.getName();
		HTMLPanel div = new HTMLPanel("");
		Button btn = new Button(tabName);
		int width = tabButtonWidth - (tab.getTablevel().intValue() * 20);
		btn.setWidth(width+"px");
		buttonList.add(btn);
		tabNameList.add(tab.getName());
		div.add(btn);
		btn.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				System.out.println(tab.getName());
				selectTab(tab.getName());
			}
		});
		tabUiNames.add(div);
	}
	
	
	public void selectTab(int index){
		System.out.println("Selecting index: "+index);
		tabUiPanel.showWidget(index);
		tabUiList[index].displayDefault();
	}
	
	public void selectTab(String tab){
		int index = tabNameList.indexOf(tab);
		selectTab(index);
	}
}
